package com.example.exchangerate;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class CurrentAdapter extends ArrayAdapter<Price> {
    private int resourceId;
    private List<Price> priceList = new ArrayList<Price>();
    private int selectedPos = 0;   //选中的位置

    class ViewHolder{
        TextView name;
        TextView val;
    }

    public CurrentAdapter(Context context, int textViewResourceId, List<Price> objects) {
        super(context, textViewResourceId, objects);
        this.resourceId = textViewResourceId;
        this.priceList = (List<Price>) objects;
    }

    public void setSelectedPos(int position) {
        selectedPos = position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Price price = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) view.findViewById(R.id.name);
            viewHolder.val = (TextView) view.findViewById(R.id.val);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.name.setText(price.getName());
        DecimalFormat ddf = new DecimalFormat("0.00000000");
        Double val = new Double(price.getVal());
        viewHolder.val.setText(ddf.format(val));
        if (selectedPos == position){
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView value = (TextView) view.findViewById(R.id.val);
            name.setTextColor(Color.RED);
            value.setTextColor(Color.RED);
        }
        else{
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView value = (TextView) view.findViewById(R.id.val);
            name.setTextColor(Color.BLACK);
            value.setTextColor(Color.BLACK);
        }
        return view;
    }
}
